package com.yundasys.lab12java.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @PACKAGE_NAME: com.yundasys.lab12java.stream
 * @NAME: StreamTest
 * @USER: jiangta
 * @DATE: 2021/7/28
 * @TIME: 8:30 上午
 * @PROJECT_NAME: springboot-labs
 */
public class StreamTest {

    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("ddd2");
        stringList.add("aaa2");
        stringList.add("bbb1");
        stringList.add("aaa1");
        stringList.add("bbb3");
        stringList.add("ccc");
        stringList.add("bbb2");
        stringList.add("ddd1");

        stringList.stream().filter(s -> s.startsWith("a"))
                .forEach(System.out::println);

        //测试sort排序
        stringList.stream().sorted().filter(s -> s.startsWith("a"))
                .forEach(System.out::println);
        System.out.println(stringList);

        //map映射
        stringList.stream().map(String::toUpperCase)
                .sorted((a,b) ->b.compareTo(a))
                .forEach(System.out::println);

        //测试match 匹配
        boolean anyMatchA = stringList.stream().anyMatch(s -> s.startsWith("a"));
        System.out.println(anyMatchA);
        boolean allStartsWithA = stringList.stream().allMatch(s -> s.startsWith("a"));
        System.out.println(allStartsWithA);
        boolean noneStartsWithZ = stringList.stream().noneMatch(s -> s.startsWith("z"));
        System.out.println(noneStartsWithZ);

        //测试count计数操作
        long startsWithB = stringList.stream().filter(s -> s.startsWith("b"))
                .count();
        System.out.println(startsWithB);

        //Reduce（规约）
        List<String> emptyList = new ArrayList<>();
        Optional<String> reduce = emptyList.stream().sorted().reduce(((s, s2) -> s + "#" + s2));
        reduce.ifPresent(System.out::println);

    }
}
